//#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int main()
{
    int i = 0;
    scanf("%d", &i);
    for (int j = 0; j < i; j++) {
        int a = 0;
        int b = 0;
        int w0, w1, w2, w3;
        scanf("%d", &a);
        scanf("%d", &b);

        scanf("%d", &w0);
        scanf("%d", &w1);
        scanf("%d", &w2);
        scanf("%d", &w3);
        int arr[4] = { 0 };
        int n = 0;
        while (a) {
            arr[n++] = a % 2;
            a /= 2;
        }
        int brr[4] = { 0 };
        n = 0;
        while (b) {
            brr[n++] = a % 2;
            b /= 2;
        }
        int crr[4] = { 0 };
        for (int n = 0; n < 4; n++) {
            if (arr[n] == 0 && brr[n] == 0) {
                crr[n] = w0;
            }
            else if (arr[n] == 0 && brr[n] == 1) {
                crr[n] = w1;
            }
            else if (arr[n] == 1 && brr[n] == 0) {
                crr[n] = w2;
            }
            else if (arr[n] == 1 && brr[n] == 1) {
                crr[n] = w3;
            }
        }
        printf("%d\n", crr[0] * 8 + crr[1] * 4 + crr[2] * 2 + crr[3] * 1);
    }

    return 0;
}